package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.database.ServerOrderResult
import com.two.screen.water.balance.bean.water.LocalOrderBean
import com.two.screen.water.balance.bean.water.PayResultBean
import com.two.screen.water.balance.bean.water.PayTypeBean
import com.two.screen.water.balance.bean.water.PayTypeBean.Companion.PAY_TYPE_JUHE
import com.two.screen.water.balance.bean.water.PayTypeBean.Companion.PAY_TYPE_SCAN
import com.two.screen.water.balance.bean.water.PayTypeBean.Companion.PAY_TYPE_SUCCESS
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.*
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit


class WaitingPayDialog24(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mWaitingPayDialogListener24: WaitingPayDialogListener24? = null

    private var mDialogCloseIv: ImageView? = null
    private var mDialogTitleTv: TextView? = null
    private var mDialogPayInfoTv: TextView? = null
    private var mDialogPayQrIv: ImageView? = null
    private var mDialogPaySuccessIv: ImageView? = null
    private var mDialogWetchIv: ImageView? = null
    private var mDialogAliIv: ImageView? = null
    private var mDialogPayTipsTv: TextView? = null
    private var mDialogAutoCloseTv: TextView? = null
    private var mDialogPaySuccessTv: TextView? = null

    private var mCurPayType: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_waiting_pay24)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mDialogCloseIv = findViewById(R.id.mDialogCloseIv)
        mDialogTitleTv = findViewById(R.id.mDialogTitleTv)
        mDialogPayInfoTv = findViewById(R.id.mDialogPayInfoTv)
        mDialogPayQrIv = findViewById(R.id.mDialogPayQrIv)
        mDialogPaySuccessIv = findViewById(R.id.mDialogPaySuccessIv)
        mDialogWetchIv = findViewById(R.id.mDialogWetchIv)
        mDialogAliIv = findViewById(R.id.mDialogAliIv)
        mDialogPayTipsTv = findViewById(R.id.mDialogPayTipsTv)
        mDialogAutoCloseTv = findViewById(R.id.mDialogAutoCloseTv)
        mDialogPaySuccessTv = findViewById(R.id.mDialogPaySuccessTv)


    }

    @SuppressLint("SetTextI18n")
    fun setShowData(payTypeVip: Int, mLocalOrderBean: LocalOrderBean? = null, data: PayResultBean? = null) {
        mCurPayType = payTypeVip
        when (mCurPayType) {
            PAY_TYPE_JUHE -> {
                mDialogPayQrIv?.visibility = View.VISIBLE
                mDialogPaySuccessIv?.visibility = View.GONE
                mDialogPaySuccessTv?.visibility = View.GONE
                mDialogTitleTv?.text = PayTypeBean.getPayTypeName(mCurPayType)
                mDialogPayQrIv?.let {
                    val createQRCodeBitmap = ZXingUtils.createQRCodeBitmap(
                        data?.qrCode, 500, "UTF-8", "H", "2", Color.BLACK, Color.WHITE, null, null, 0.2f
                    )
                    it.setImageBitmap(createQRCodeBitmap)
                }
                mDialogPayInfoTv?.text = "实付￥${mLocalOrderBean?.totalPrice}"
                mDialogWetchIv?.visibility = View.VISIBLE
                mDialogAliIv?.visibility = View.VISIBLE
                mDialogAutoCloseTv?.visibility = View.GONE
                mDialogPayTipsTv?.text = "扫码，支付订单"
                ScannerGunManager.getInstance().setScanListener(null)
                mDialogCloseIv?.setOnClickListener { mWaitingPayDialogListener24?.dismiss() }
            }
            PAY_TYPE_SCAN -> {
                mDialogPayQrIv?.visibility = View.VISIBLE
                mDialogPaySuccessIv?.visibility = View.GONE
                mDialogPaySuccessTv?.visibility = View.GONE
                mDialogTitleTv?.text = PayTypeBean.getPayTypeName(mCurPayType)
                mDialogPayQrIv?.setImageResource(R.drawable.waiting_pay_scan_qr)
                mDialogPayInfoTv?.text = "实付￥${mLocalOrderBean?.totalPrice}"
                mDialogWetchIv?.visibility = View.GONE
                mDialogAliIv?.visibility = View.GONE
                mDialogAutoCloseTv?.visibility = View.GONE
                mDialogPayTipsTv?.text = "请出示付款码"
                //初始化 盒子扫码键盘输入
                ScannerGunManager.getInstance().setScanListener { code ->
                    var newCode = code.trim().replaceFirst("?", "")
                    //CommUtils.println("getAuthCodeSuccess->", newCode)
                    if (newCode.isNotBlank()) mWaitingPayDialogListener24?.getAuthCodeSuccess(newCode)
                }
                mDialogCloseIv?.setOnClickListener { mWaitingPayDialogListener24?.dismiss() }
            }
            PAY_TYPE_SUCCESS -> {
                mDialogPaySuccessTv?.visibility = View.VISIBLE
                mDialogPaySuccessIv?.visibility = View.VISIBLE
                mDialogPayQrIv?.visibility = View.GONE
                mDialogPayTipsTv?.visibility = View.GONE
                mDialogPaySuccessIv?.setImageResource(R.drawable.waiting_pay_success)
                mDialogWetchIv?.visibility = View.GONE
                mDialogAliIv?.visibility = View.GONE
                ScannerGunManager.getInstance().setScanListener(null)
                mDialogAutoCloseTv?.visibility = View.VISIBLE
                mDialogCloseIv?.setOnClickListener { mCloseDisposable?.dispose();mWaitingPayDialogListener24?.autoClosePay() }
            }
            else -> {}
        }
    }

    private var mCloseDisposable: Disposable? = null
    private var mCount = 15L
    @SuppressLint("CheckResult", "SetTextI18n") fun setPaySuccess() {
        setShowData(PAY_TYPE_SUCCESS, null, null)
        //TODO:开始倒计时
        mCloseDisposable?.dispose()
        mCloseDisposable = Flowable
            .intervalRange(1, mCount, 0, 1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext { s ->
                mDialogAutoCloseTv?.text = "${mCount - s}秒后自动关闭"
            }
            .doOnComplete {
                mWaitingPayDialogListener24?.autoClosePay()
                mCloseDisposable?.dispose()
            }.subscribe()

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            return true
        }
        var ret = super.onKeyDown(keyCode, event)
        ScannerGunManager.getInstance().dispatchKeyEvent(keyCode, event);
        return ret;
    }

    fun setWaitingPayDialog24Listener(l: WaitingPayDialogListener24) {
        mWaitingPayDialogListener24 = l
    }

    abstract class WaitingPayDialogListener24 {
        open fun dismiss() {}
        open fun getAuthCodeSuccess(code: String) {}
        open fun autoClosePay() {}
    }

    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
